/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 2e5;

int a[maxn + 1], f[maxn + 1];
std::priority_queue<int> q[maxn + 1];

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      f[i] = 0;
      cin >> a[i];
      while (!q[i].empty()) {
        q[i].pop();
      }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      if (q[a[i]].empty()) {
        q[a[i]].push(f[i - 1] - i);
      } else {
        f[i] = q[a[i]].top() + i + 1;
        q[a[i]].push(f[i - 1] - i);
      }
      f[i] = std::max(f[i], f[i - 1]);
      ans = std::max(ans, f[i]);
    }
    cout << ans << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("C. Tenzing and Balls.in", "r", stdin);
  freopen("C. Tenzing and Balls.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
